<?php
// head control
Magnum::import('head/manager');
Magnum::import('body/manager');
/**
 * view renderer
 * @uses views/ as the main folder
 * @uses /themes/[SITE_THEME]/site.php for themes
 * also provide functions to quickly render stuff
 */
class Magnum_View extends Magnum_Utility_Stack_Manager
{
    /**
     * head control
     * @var Magnum_Head_Manager
     */
    protected $_header = null;


    /**
     * body control
     * @var Magnum_Body_Manager
     */
    protected $_body = null;


    // constructor
    public function __construct()
    {
        // header
        $this->_header = new Magnum_Head_Manager($this);
        $this->_body = new Magnum_Body_Manager($this);

    }// end __construct


    /**
     * get head object
     * @return Magnum_Head_Manager
     */
    public function getHeader()
    {
        return $this->_header;

    }// end getHeader


    /**
     * get body object
     * @return Magnum_Body_Manager
     */
    public function getBody()
    {
        return $this->_body;

    }// end getBody

    /*-----------------------------[the inclusion and rendering]-----------------------------*/


    /**
     * add template
     * @param str $key the key of the template
     */
    public function addTemplate($key = 'body', $template = '', $type = 'APPEND')
    {
        $this->_body->addTemplate($key, $template, $type);
        return $this;

    }// end addTemplate


    /**
     * add src
     * @param str $key the key of the template
     */
    public function addSrc($key = 'body', $template = '', $type = 'APPEND')
    {
        // directly add it...
        $this->_body->addSrc($key, $template, $type);
        return $this;

    }// end addSrc

    // get the 'body' key
    public function body($print = 1)
    {
        return $this->renderView('body');

    }// end getBody


    // get header all
    public function header($print = 1)
    {
        return $this->_header->export($print);

    }// end header


    /**
     * reander view by given key
     * @param str $key the key to render
     * @param bool $print if true, print.
     * @return mixed...
     */
    public function renderView($key, $print = 1)
    {
        // log
        $this->_log('Start rendering: ' . $key, __function__);

        // if print is false
        if (!$print) ob_start();

        // get source...
        $pointSrc = $this->_body->get($key, 1)->getItems();
        if (is_array($pointSrc)) {
            foreach ($pointSrc as $point) {
                // render it...
                if ($point['type'] == 'FILE') {
                    $this->_log('> Render file: <small>' . $point['src'] . '</small>', __function__);
                    include $point['src'];
                } else {
                    $this->_log('> Render raw source', __function__, $point['src']);
                    echo $point['src'];
                }
            }
        }

        $this->_log('End rendering: ' . $key, __function__);

        if (!$print) return ob_get_clean();
        return $this;

    }// end renderView



    /**
     * this is the final rendering!!
     * @return void
     */
    public function render($template = 'site.php')
    {
        include $this->_body->_getViewTemplate($template);
        return;

    }// end render


    /**
     * log source
     * @param str $message, the message
     * @param str $func the function name
     * @param mixed $params the params, can be anything
     * @param bool $isErr if true, it's an error!
     */
    protected function _log($message, $func = 'N/A', $params = null, $isErr = false)
    {
        if (!defined('_LOG') || !_LOG) {
            return $this;
        }
        // construct the log from here...
        $details = array(
            'class' => __class__,
            'func' => $func,
            'params' => $params
        );

        __log($message, 'view', $details, $isErr);
        return $this;

    }// end _log

    /*-----------------------------[short hands for rendering]-----------------------------*/

    /**
     * build image tag
     * @param str $src the source
     * @param bool $isTheme if true use theme
     * @param str $extra extra things go into the tag
     * @param bool $print if true, print
     * @return str image tag HTML
     */
    public function _img($src, $print = true, $isTheme = true, $extra = null)
    {
        if (strpos($src, '://') === false) {
            if ($isTheme) {
                $src = THEMEURL . 'images/' . $src;
            } else {
                // internal
                $src = BASEURL . $src;
            }
        }

        $img = '<img src="' . $src . '" ' . $extra . '/>';

        if ($print) {
            echo $img;
        }

        return $img;

    }// end _img


    /**
     * returns only URL to the base
     * @param str  $path    path to the file/page
     * @param bool $isTheme if true, use theme...
     * @param bool $print if true, print
     * @return str the path
     */
    public function _getUrl($path, $print = true, $isTheme = false)
    {
        // check if http/https...or rather ://
        if (strpos($path, '://') === false) {
            if ($isTheme) {
                $path = $this->_view->_themePath($path);
            } else {
                $path = BASEURL . $path;
            }
        }

        if ($print) {
            echo $path;
        }

        return $path;

    }// end _getUrl


    /**
     * quickly compose the url
     */
    public function _url($path, $title = null, $isTheme = false, $print = true, $target = '_blank', $extra = null)
    {
        $path = $this->_getUrl($path, false, $isTheme);
        if (empty($title)) {
            $title = $path;
        }
        // make it...
        $url = '<a href="' . $path . '" target="' . $target . '" ' . $extra . '>' . $title . '</a>';

        if ($print) {
            echo $url;
        }

        return $url;

    }// end _url

    /**
     * make scripts tag
     * @param str  $src source code or path
     * @param bool $print if true, print
     * @return str
     */
    public function _js($src, $print = true)
    {
        $js = $this->_header->js()->_getMarkup($src);

        if ($print) {
            echo $js;
        }

        return $js;

    }// end _js


    /**
     * css...
     * @param str $src   source/file
     * @param bool $print if true, print
     * @param str $media media type
     * @return self
     */
    public function _css($src, $media = 'screen')
    {
        $js = $this->_header->css()->_getMarkup($src, $media);

        if ($print) {
            echo $js;
        }

        return $js;

    }// end _css

}// end Magnum_View
?>